using UnityEngine;
using System.Collections;

/// <summary>
/// Manages the player controls
/// </summary>
public class playerControls : MonoBehaviour 
{
    public GameObject currentRoom;

    private Transform myTransform;

    private const float VELOCITY = 10.0f;
    private const float NEAR_END_OF_ROOM = 10.0f;
   
    /// <summary>
    /// Initializes the player and its attributes
    /// </summary>
    void Start()
    {
        myTransform = transform;
    }
	    	
    /// <summary>
    /// Updates all changes on the player
    /// </summary>
	void Update () 
    {
        movePlayer();
	}

    /// <summary>
    /// Moves the player using the arrows
    /// </summary>
    private void movePlayer()
    {
        if (Input.GetKey(KeyCode.LeftArrow))
            myTransform.position += new Vector3(-Time.deltaTime * VELOCITY, 0, 0);
        if (Input.GetKey(KeyCode.RightArrow))
            myTransform.position += new Vector3(Time.deltaTime * VELOCITY, 0, 0);
        if (Input.GetKey(KeyCode.UpArrow))
            myTransform.position += new Vector3(0, 0, Time.deltaTime * VELOCITY);
        if (Input.GetKey(KeyCode.DownArrow))
            myTransform.position += new Vector3(0, 0, -Time.deltaTime * VELOCITY);
    }

    /// <summary>
    /// Tests if the player is near the end of the room
    /// depending on the constant NEAR_END_OF_ROOM
    /// </summary>
    /// <returns></returns>
    public bool isOnLimitsOfCurrentRoom()
    {
        if ((myTransform.position.x -
                (currentRoom.transform.position.x -
                 currentRoom.transform.localScale.x / 2))
                 < NEAR_END_OF_ROOM)
            return true;
        if (((currentRoom.transform.position.x +
              currentRoom.transform.localScale.x / 2)
              - myTransform.position.x)
                 < NEAR_END_OF_ROOM)
            return true;
        return false;
    }
}
